package com.rocksea.app.rslib;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;

public class JySlgQ extends JyCurve
{
	public JySlgQ(JyDoc doc)
	{
		super(doc);

		m_strScaleX = "Q (kN)";
		m_strScaleY = "s (mm)";
		m_strTitle = "s-lgQ曲线";
	}

	@Override
	protected void drawCurve(Canvas canvas)
	{
		Paint curPaint = new Paint();
		if (m_jyDoc == null)
			return;

		int nLoadGradeCount = m_jyDoc.getJyDataInfo().nLoadGradeCount;
		PointF ptStart = getZeroPoint();

		// ����
		curPaint.set(m_penLoad);
		curPaint.setColor(m_style.loadCurveStyle.lineColor);
		for (int i = 0; i < nLoadGradeCount; i++)
		{
			PointF ptEnd = getGradePoint(i);
			canvas.drawLine(ptStart.x, ptStart.y, ptEnd.x, ptEnd.y, curPaint);

			ptStart.x = ptEnd.x;
			ptStart.y = ptEnd.y;
		}

		// ����
		curPaint.set(m_penDot);
		curPaint.setColor(m_style.dotStyle.dotColor);
		RectF ellipseRect = new RectF();
		for (int i = 0; i < nLoadGradeCount; i++)
		{
			if (m_jyDoc.getJyData().get(i).fQGrade <= 1f)
				continue;

			PointF pt = getGradePoint(i);

			ellipseRect.left = pt.x - m_style.dotStyle.dotDiameter / 2f;
			ellipseRect.top = pt.y - m_style.dotStyle.dotDiameter / 2f;
			ellipseRect.bottom = m_style.dotStyle.dotDiameter + ellipseRect.top;
			ellipseRect.right = m_style.dotStyle.dotDiameter + ellipseRect.left;

			if (m_style.dotStyle.isFilled)
			{
				curPaint.setStyle(Paint.Style.FILL);
				canvas.drawOval(ellipseRect, curPaint);
			} else
				canvas.drawOval(ellipseRect, curPaint);
		}
	}

	@Override
	protected void drawXCoordinate(Canvas canvas)
	{
		Paint curPaint = new Paint();
		super.drawXCoordinate(canvas);
		// д����ֵ
		curPaint.setColor(m_style.scaleFont.fontColor);

		// д���ĺ�����ֵ
		String strText = String.format("%.0f", m_fXMax);

		curPaint.setTypeface(m_fontScale);
		// curPaint.setStrokeWidth(m_style.scaleFont.fontHeight);
		curPaint.setTextSize(m_style.scaleFont.fontHeight);

		Rect rectText = new Rect();
		curPaint.getTextBounds(strText, 0, strText.length(), rectText);

		float fWidth = m_rcCurve.width() * 9f / 10f;
		PointF pointText = new PointF();
		pointText.x = m_rcCurve.left + fWidth - rectText.width() / 2f;
		pointText.y = m_isUp ? (m_rcCurve.bottom + m_style.longScale
				/ 2f + rectText.height())
				: (m_rcCurve.top - m_style.longScale * 1.5f);

		canvas.drawText(strText, pointText.x, pointText.y, curPaint);

		if (m_jyDoc == null)
			return;

		float fMaxRight = pointText.x - rectText.width() / 2f;
		float fMinLeft = m_rcCurve.left;

		for (int i = 0; i < m_jyDoc.getJyDataInfo().nLoadGradeCount; i++)
		{
			float fQ = m_jyDoc.getJyData().get(i).fQGrade;
			if (fQ <= 1f)
				continue;

			pointText.x = getGradePoint(i).x;
			strText = String.format("%.0f", fQ);

			curPaint.setTypeface(m_fontScale);
			// curPaint.setStrokeWidth(m_style.scaleFont.fontHeight);
			curPaint.setTextSize(m_style.scaleFont.fontHeight);

			curPaint.getTextBounds(strText, 0, strText.length(), rectText);

			if ((pointText.x - rectText.width() / 2f > fMinLeft + 1f)
					&& (pointText.x + rectText.width() / 2f < fMaxRight - 1f))
			{
				canvas.drawText(strText, pointText.x - rectText.width()
						/ 2f, pointText.y, curPaint);
				fMinLeft = pointText.x + rectText.width() / 2f;
			}
		}

		// ����������ߣ��Ͳ��û�����̶���
		if (m_style.gridNum > 0)
			return;

		// ������̶�
		curPaint.set(m_penCoordinate);
		curPaint.setColor(m_style.coordinateStyle.lineColor);
		curPaint.setStrokeWidth(0f);

		float y1 = m_isUp ? (float) m_rcCurve.bottom : m_rcCurve.top;
		float y2 = m_isUp ? y1 + m_style.longScale : y1 - m_style.longScale;
		for (int i = 0; i < m_jyDoc.getJyDataInfo().nLoadGradeCount; i++)
		{
			if (m_jyDoc.getJyData().get(i).fQGrade <= 1f)
				continue;

			float x = getGradePoint(i).x;
			if (x > m_rcCurve.left)
			{
				canvas.drawLine(x, y1, x, y2, curPaint);
			}
		}
	}

	protected PointF getGradePoint(int nGrade)
	{
		if (nGrade >= m_jyDoc.getJyData().size())
			return (new PointF(0f, 0f));

		float fWidth = m_rcCurve.width() * 0.9f;
		float fHeight = m_rcCurve.height() * 0.9f;
		float fXFullScale = (float) (Math.log10(m_fXMax) - Math.log10(m_fXMin));
		float fYFullScale = m_fYMax - m_fYMin;

		float fQ = m_jyDoc.getJyData().get(nGrade).fQGrade;
		float fSEnd = m_jyDoc.getJyData().get(nGrade).fSEnd;

		PointF pt = new PointF();
		if (fQ < m_fXMin)
		{
			pt.x = m_rcCurve.left;
		} else
		{
			double fXDelta = (Math.log10(fQ) - Math.log10(m_fXMin))
					/ fXFullScale;
			pt.x = m_rcCurve.left + fWidth * (float) fXDelta;
		}

		float fYDelta = (fSEnd - m_fYMin) / fYFullScale;
		pt.y = m_isUp ? m_rcCurve.bottom - fHeight * fYDelta
				: m_rcCurve.top + fHeight * fYDelta;

		return pt;
	}

	@Override
	protected void calcMaxMinValue()
	{
		super.calcMaxMinValue();

		if (m_jyDoc.getJyDataInfo().nGradeCount < 2)
			return;

		double fQ1 = 1d;
		for (int i = 0; i < m_jyDoc.getJyDataInfo().nGradeCount; i++)
		{
			if (m_jyDoc.getJyData().get(i).fQGrade > fQ1)
			{
				fQ1 = m_jyDoc.getJyData().get(i).fQGrade;
				break;
			}
		}

		double fQ2 = m_jyDoc.getJyDataInfo().fQMax;
		double fDelta = (Math.log10(fQ2) - Math.log10(fQ1)) / 2d;
		double fLogQStart = Math.log10(fQ1) - fDelta;
		m_fXMin = (float) (Math.pow(10d, fLogQStart));
	}
}
